/**
 * Created by fdg282 on 2017/9/20.
 */
$(function(){
    appendSpec(main_spec_id,main_spec_color_id);
    detailbtns();//底部按钮处理
    // mod FDG416 2018/2/1 ID485 【商城需求】商品详情页按钮调整 start
    if(1==is_collection){
        $('.js_inset_hart').children('.icon').css({
            'background':'url('+image_bath_url+'/static/pc/image/list/xin-over.png) no-repeat center'
        });
    }
    // mod FDG416 2018/2/1 ID485 【商城需求】商品详情页按钮调整 end
   
});

/*mod by FDG416 ID527 【pc商城优化】修改滚动条 2018/02/26 start*/
//调用滚动条插件
$('#spec_color').niceScroll({
    cursorcolor: "#000",//滚动条的颜色
    cursoropacitymax: 1, //滚动条的透明度，从0-1
    touchbehavior: false, //使光标拖动滚动像在台式电脑触摸设备
    cursorwidth: "1px", //滚动条的宽度
    cursorborder: "0", // 游标边框css定义
    cursorborderradius: "5px",//以像素为光标边界半径  圆角
    autohidemode: false, //是否隐藏滚动条  true的时候默认不显示滚动条，当鼠标经过的时候显示滚动条
    zindex: "auto",//给滚动条设置z-index值
    railpadding: { top: 0, right: 0, left: 0, bottom: 0 }//滚动条的位置
});
//隐藏横向滚动条（主要是IE）
$('.nicescroll-rails-hr > .nicescroll-cursors').css('display','none');
/*mod by FDG416 ID527 【pc商城优化】修改滚动条 2018/02/26 end*/

//商品规格颜色处理
function appendSpec(spec_id,spec_color_id){
    //alert(main_goods_id);
    //alert(main_spec_id);
	var checked_color = spec_color_id;
    $.post("/goods/spec-list",{spec:spec_id,spec_color:spec_color_id,main_goods_id:main_goods_id},success,'json');
    return false;
    function success(data){
        if(data.code == 200){
            //console.log(55);
            //填充规格
            var specHtml            = '';
            var specColorHtml       = '';
            var num = 0,num_2=0;
            if(data.data.spec.length <4){   //填充ul
                $.each(data.data.spec,function(i,id){
                    if(spec_id && spec_id != 0){
                        specHtml += '<li class="item '+(id.spec == spec_id ? 'style_active' : '')+'" g_spec="'+id.spec+'">'+id.spec_val+'</li>';
                    }else{
                        specHtml += '<li class="item '+(num == 0 ? 'style_active' : '')+'" g_spec="'+id.spec+'">'+id.spec_val+'</li>';
                        num++;
                    }
                });
                $("#spece_min").html(specHtml);



            }else{              //填充select
                specHtml    ='';
               // specHtml += '<option value="0">请选择规格</option>';
                $.each(data.data.spec,function(i,id){
                    //console.log(id);
                    if(spec_id && spec_id != 0){
                        specHtml += '<option '+(id.spec == spec_id ? 'selected' : '')+' value="'+id.spec+'">'+id.spec_val+'</option>';
                    }else{
                        specHtml += '<option value="'+id.spec+'">'+id.spec_val+'</option>';
                    }
                });
                $('#spece_max').html(specHtml);
            }

            //填充颜色
            $.each(data.data.specColor,function(i,id){
                //
                if(spec_color_id && spec_color_id != 0){
                    specColorHtml +='<li class="item d_l '+(id.spec_color == spec_color_id ? 'color_active' : '')+'" g_color="'+id.spec_color+'">'
                    if(id.specColorRelation.color_pic)
                        specColorHtml +='<img src="'+image_bath_url+'/'+id.specColorRelation.color_pic+'" alt="'+id.specColorRelation.spec_name+'"/>';

                    specColorHtml+='<div class="shadebox tips mid" ><p>';
                    if(id.specColorRelation.remark){
                        specColorHtml += id.specColorRelation.remark+'<br/>'+id.specColorRelation.spec_name+'</p></div></li>'
                    }else{
                        specColorHtml += id.specColorRelation.spec_name+'</p></div></li>'
                    }
                }else{
                	if(!checked_color || checked_color == 0){
                		checked_color = id.spec_color;
                	}
                    specColorHtml +='<li class="item d_l '+(num_2 == 0 ? 'color_active' : '')+'" g_color="'+id.spec_color+'">';
                    if(id.specColorRelation.color_pic)
                        specColorHtml +='<img src="'+image_bath_url+'/'+id.specColorRelation.color_pic+'" alt="'+id.specColorRelation.spec_name+'"/>';
                    specColorHtml +='<div class="shadebox tips mid" ><p>';
                    if(id.specColorRelation.remark){
                        specColorHtml += id.specColorRelation.remark+'<br/>'+id.specColorRelation.spec_name+'</p></div></li>'
                    }else{
                        specColorHtml += id.specColorRelation.spec_name+'</p></div></li>'
                    }
                    num_2++;
                }
            });

            $('#spec_color').html(specColorHtml);
            //colorList();

            var colorList = $(".detail_color ul li");
            var styleList1 = $(".style_list_2 li");
            var styleList2 = $(".detail_style select");

            if(colorList.length>=12){
                colorList.parents(".detail_color").addClass("color_scroll")
            }

            if(styleList1.length>4){
                console.log(styleList1);
                styleList1.parents('ul').hide().siblings('.s-1').show();
            }else {
                styleList1.parents('ul').show().siblings('.s-1').hide();
            }
            //点击规格和颜色
            $(".style_list_2 li, .detail_color li").click(function(){
                var class_id = $(this).parents('ul').attr('id');
                if(class_id == 'spec_color'){
                    appendSpec(spec_id,$(this).attr('g_color'));
                }else{
                    appendSpec($(this).attr('g_spec'),0);
                }
                if($(this).hasClass("style_active")){

                }else{
                    $(this).addClass(class_id == 'spec_color' ? 'color_active' : 'style_active').siblings('li').removeClass(class_id == 'spec_color' ? 'color_active' : 'style_active');
                    //spec = $('.style_list_2').find('li.style_active').attr('g_spec');
                    //color = $(".color_list").find('li.color_active').attr('g_color');

                    //appendPrice(spec,color);
                }
            });
            //填充价格等信息 sale_price,market_price,sku,series
            pushPrice(data.data.nowGoodsSale[0].sale_price,data.data.nowGoodsSale[0].market_price,data.data.nowGoodsSale[0].sku,data.data.nowGoodsSale[0].series,data.data.nowGoodsSale[0].hidden_price);
            //填充优惠券 和 促销信息
            getSale(spec_id,checked_color,main_goods_id);
            getCoupon(spec_id,checked_color,main_goods_id);
            //填充资源文件
            getSource(spec_id,checked_color);
            $('.main_goods_span').attr('data-shop',data.data.nowGoodsSale[0].goods_id);
            //  add FDG367 2018/2/5 ID493 【商城需求】商品图片上增加图片打标 start
            if(data.data.nowGoodsSale[0].markImgInfo){
                $('.item_mark_info_content_sku').html('<img src="'+image_bath_url+'/'+data.data.nowGoodsSale[0].markImgInfo.img_url+'">');
            }else{
                $('.item_mark_info_content_sku').html('');
            }
        }else{
            alert(data.msg);
        }
    }
}

//填充静态资源
function getSource(spec,color){
    $.post("/goods/source-list",{spec:spec,spec_color:color,main_goods_id:main_goods_id},success,'json');
    return false;
    function success(data){
        if(data.code == 200){
            $('.left_img_tab').empty();
            $('.left_img_big').find('.item_img_show').empty();
            $('.js_video_box').empty();
            if($('.js_video_box').hasClass('hide')){

            }else{
                $('.js_video_box').addClass('hide');
            }
            imgShow(data.val);
            //$('.main_goods_span').attr('data-shop',price[0]);
        }
    }
}

//细节图片展示
function imgShow(videos){
    //获取小图片
    //模拟ajax
    if(videos !== null){
        var bigImgbox =  $(".left_img_big");
        var miniImgbox = $(".left_img_tab");
        var bigVideobox = $(".js_video_box");
        var imgList;
        $.each(videos,function(i,id){
            if(id.source_type == 0){
                id.source_link=null
            }
            var img_url = id.source_url;
            //var img_url = checkImgExists(id.source_url);
            imgList = '<li id='+id.id+'><div><a href="javascript:;"><img src='+img_url+' mid='+img_url+' big='+img_url+' alt='+id.goods_name_cn+'></a><video  class="video-js  vjs-big-play-centered video detail_video " controls preload="none"  poster="" src='+id.source_link+' type="video/mp4"></video></div></li>';
            miniImgbox.append(imgList);
           //开始渲染第一个图片、视频
            var tabImg = $(".left_img_tab li:first-child").addClass('tb-selected').find('img').attr('mid');
            var tabvideo = $(".left_img_tab li:first-child").find('video').attr('src');
            var imgUrl = '<img src='+tabImg+' rel='+tabImg+' class="jqzoom mid" />';
            var videoUrl = '<video  class="video-js  vjs-big-play-centered video detail_video" controls preload="none"  poster="" src='+tabvideo+' type="video/mp4" id="dVideo01"></video>';
            bigImgbox.find('.item_img_show').html(imgUrl);
            bigVideobox.html(videoUrl);
            $(".jqzoom").imagezoom();
            //视频播放
            if(bigVideobox.find('video').attr("src")!='null'){
                $('.icon-video').show();
                $(".js_video_box video").attr('src',$(".tb-selected").children('video').attr("src")).attr('id','dVideo'+$(this).parents('.tb-selected').attr("id"));
                if($('.js_video_box').hasClass('hide')){
                    $(".icon-video").click(function(){
                        //$(".zoomDiv,.zoomMask").hide();
                        $(this).hide();
                        $('.item_img_show').hide().siblings('.js_video_box').removeClass('hide');
                        var videoUrl = bigVideobox.children('video').attr('src');
                        var videoUrlID = bigVideobox.children('video').attr('id');
                        videojs(videoUrlID,{}, function() {
                            var myPlayer = this;
                            $(".js_video_box video").attr("src", videoUrl);
                            myPlayer.src(videoUrl);
                            myPlayer.load(videoUrl);
                            myPlayer.play();
                            myPlayer.on("ended", function(){
                                $(".js_icon").show();
                                $('.item_img_show').show().siblings('.js_video_box').addClass('hide');

                            });
                            myPlayer.on("pause",function(){
                                $(".js_icon").show();
                            })
                        });
                    });
                }else {

                }
            }else{
                $(".left_img_big .icon").removeClass('icon-video');
            }
             //小图切换
        $(".left_img_tab li a").mouseenter(function(){
              $(".left_img_big .icon").addClass('icon-video');
                $('.item_img_show').show().siblings('.js_video_box').addClass('hide');
                $('.icon-video').hide();
                //$(".zoomDiv,.zoomMask").show();
                $(this).parents("li").addClass("tb-selected").siblings('li').removeClass("tb-selected");
                //赋值img属性
                $(".jqzoom").attr('src',$(this).find("img").attr("mid")).attr('rel',$(this).find("img").attr("big"));

                //视频播放
                if($(this).siblings('video').attr("src")!='null'){
                    $('.icon-video').show();
                    $(".js_video_box video").attr('src',$(this).siblings('video').attr("src")).attr('id','dVideo'+$(this).parents('li').attr("id"));
                    if($('.js_video_box').hasClass('hide')){
                        $(".icon-video").click(function(){
                            //$(".zoomDiv,.zoomMask").hide();
                            $(this).hide();
                            $('.item_img_show').hide().siblings('.js_video_box').removeClass('hide');
                            var videoUrl = $(".tb-selected").children('video').attr("src");
                            var  videoUrlID = bigVideobox.children('video').attr('id');
                            videojs(videoUrlID,{}, function() {
                                var myPlayer = this;
                                $(".js_video_box video").attr("src", videoUrl);
                                myPlayer.src(videoUrl);
                                myPlayer.load(videoUrl);
                                myPlayer.play();
                                myPlayer.on("ended", function(){
                                    $(".js_icon").show();
                                    $('.item_img_show').show().siblings('.js_video_box').addClass('hide');

                                });
                                myPlayer.on("pause",function(){
                                    $(".js_icon").show();
                                })
                            });
                        });
                    }else {

                    }
                }else{
                    $(".left_img_big .icon").removeClass('icon-video');
                }
            });
            /*$(".left_img_big>.img").mouseenter(function(){
             $(".jqzoom").imagezoom();
             });*/
        });
    }
}

function checkImgExists(imgurl){
    var default_img = bath_url + "/uploads/2017-10-20/150848897870422.png";
    var width_img;
    width_img = getImageWidth(imgurl,imgSS);
    if(eval(width_img) > 0){
        return imgurl;
    }else{
        return default_img;
    }
}

function imgSS(width){
    console.info(width);
    return width;
}

function getImageWidth(url){
    var img = new Image();
    img.src = url;

    // 如果图片被缓存，则直接返回缓存数据
    if(img.complete){
        callback(img.width);
    }else{
        // 完全加载完毕的事件
        img.onload = function(){
            callback(img.width);
        }
    }

}

function colorList(){

    var colorList = $(".detail_color ul li");
    var styleList1 = $(".style_list_2 li");
    var styleList2 = $(".detail_style select");
    var priceList = $(".price_info");
    var sale_price = $('span.price').find('span').html();
    var market_price = $('span.price').find('del').eq(1).html();

    var colorSize = colorList.length;
    var spec = 0;
    var color = 0;
    if(colorSize>=12){
       colorList.parents(".detail_color").addClass("color_scroll")
    }
    if(styleList1.length>4){
        styleList1.parents('ul').hide().siblings('.s-1').show();
    }else {
        styleList1.parents('ul').show().siblings('.s-1').hide();
    }

    if(eval(sale_price) == eval(market_price)){
        $('span.price').find('del').hide();
    }

    $('span.price').find('span').html(toThousands(sale_price));
    $('span.price').find('del').eq(1).html(toThousands(market_price));
    /*只有规格与颜色都选择时才显示一个价格，其他显示价格区间*/
    //选择颜色样式
    priceList.find("span.p_1").show().siblings("span.p_2").hide();
    color = $(".color_list").find('li.color_active').attr('g_color');
    spec = $('.style_list_2').find('li.style_active').attr('g_spec');
    colorList.each(function(){
        $(this).click(function () {
            if($(this).hasClass('color_active')){

            }else{
                $(this).addClass("color_active").siblings(".item").removeClass("color_active");
                spec = $('.style_list_2').find('li.style_active').attr('g_spec');
                color = $(".color_list").find('li.color_active').attr('g_color');
                //appendPrice(spec,color);
            }

        })
    });
    //点选样式
    //styleList1.each(function(){
    //   $(this).click(function(){
    //
    //       if($(this).hasClass("style_active")){
    //
    //       }else{
    //           $(this).addClass("style_active").siblings('li').removeClass("style_active");
    //           spec = $('.style_list_2').find('li.style_active').attr('g_spec');
    //           color = $(".color_list").find('li.color_active').attr('g_color');
    //
    //           //appendPrice(spec,color);
    //       }
    //
    //   })
    //});
    //选择下拉样式
    styleList2.change(function(){
        spec = $(this).val();
        color = $(".color_list").find('li.color_active').attr('g_color');
        //appendPrice(spec,color);
    })
}

/*function appendPrice(spec,color){
    var min_price = 0;
    var max_price = 0;
    var main_goods_string = main_goods_id;

    var detail_color = $('.detail_color').length;//判断是否存在颜色规格

    getSale(spec,color,main_goods_string);
    getCoupon(spec,color,main_goods_string);
    if(!isNaN(spec) && isNaN(color)){
        if(detail_color == 0){
            var price = getSpec(spec,color);
            $.post("/goods/source-list",{spec:spec,main_goods_id:main_goods_string},success,'json');
            return false;
            function success(data){
                if(data.code == 200){

                    $('.left_img_tab').empty();
                    $(".left_img_big").find('.item_img_show').empty();
                    $(".js_video_box").empty();
                    if($('.js_video_box').hasClass('hide')){

                    }else{
                        $('.js_video_box').addClass('hide');
                    }
                    imgShow(data.val);
                    //pushPrice(price[3],price[4],price[5]);
                    pushPrice(price[3],price[4],price[5],price[6]);
                }
            }
        }else{
            var spec_price = getPrice(spec,1);
            min_price = Math.min.apply(null,spec_price);
            max_price = Math.max.apply(null,spec_price);
            pushPriceZone(min_price,max_price);
        }
    }
    if(isNaN(spec) && !isNaN(color)){
        spec_price = getPrice(color,2);
        min_price = Math.min.apply(null,spec_price);
        max_price = Math.max.apply(null,spec_price);
        pushPriceZone(min_price,max_price);
    }
    if(isNaN(spec) && isNaN(color)){
        var str = $('.min_max_sale').attr('g_min_sale');
        console.log(str)
        var max_min_data = splitString(str);
        pushPriceZone(max_min_data[0],max_min_data[1]);
    }
    if(!isNaN(spec) && !isNaN(color)){
        var price = getSpec(spec,color);
        $.post("/goods/source-list",{spec:spec,main_goods_id:main_goods_string,spec_color:color},success,'json');
        return false;
        function success(data){
            if(data.code == 200){
                $('.left_img_tab').empty();
                $('.left_img_big').find('.item_img_show').empty();
                $('.js_video_box').empty();
                if($('.js_video_box').hasClass('hide')){

                }else{
                    $('.js_video_box').addClass('hide');
                }

                imgShow(data.val);
                //pushPrice(price[3],price[4],price[5]);
               // pushPrice(price[3],price[4],price[5],price[6]);  处理商品的信息TODO
                $('.main_goods_span').attr('data-shop',price[0]);
            }
        }
    }
}*/

//置换价格
function pushPrice(sale_price,market_price,sku,series,hidden_price){
	switch(hidden_price){
		case '1':
			$(".price_info").find("span.p_1").text('请咨询客服');
	        $(".price_info").find("span.p_1").css('font-size','1.6rem');
			break;
		case '2':
		case '3':
		case '4':
			$(".price_info").find("span.p_1").children().eq(0).html(toThousands(sale_price,2));
			if($(".price_info").find("span.p_1").children().eq(1).hasClass('price_text') == false && hidden_price != '2'){
				$(".price_info").find("span.p_1").children().eq(0).after('<span class="price_text"> '+PRICE_SUFFIX+'</span>');
			}
			if(hidden_price == '3'){
				$(".price_info").find("del").eq(1).html(toThousands(market_price,2));
			}else{
				$(".price_info").find("del").eq(1).html(toThousands(market_price,2)+PRICE_SUFFIX);
			}
			if((eval(sale_price) == eval(market_price)) || eval(market_price) == 0){
		        $(".price_info").find("del").hide();
		    }else{
		        $(".price_info").find("del").show();
		    }
	        $(".price_info").find("span.p_1").css('font-size','2.4rem');
			break;
		default:
			$(".price_info").find("span.p_1").children().eq(0).html(toThousands(sale_price,2));
			$(".price_info").find("del").eq(1).html(toThousands(market_price,2));
			if((eval(sale_price) == eval(market_price)) || eval(market_price) == 0){
		        $(".price_info").find("del").hide();
		    }else{
		        $(".price_info").find("del").show();
		    }
	        $(".price_info").find("span.p_1").css('font-size','2.4rem');
			break;
	}
    $(".detail_info_num").find("span").html(sku);
    $('.detail_info_series').text(series != '' && series != null ? series  : '');
    $(".price_info").find("span.p_1").show().siblings("span.p_2").hide();
}

/*
function pushPrice(sale_price,market_price,sku){
    $(".price_info").find("span.p_1").children().eq(0).html(toThousands(sale_price,2));
    $(".price_info").find("del").eq(1).html(toThousands(market_price,2));
    $(".detail_info_num").find("span").html(sku);
    if(eval(sale_price) == eval(market_price)){
        $(".price_info").find("del").hide();
    }else{
        $(".price_info").find("del").show();
    }
    $(".price_info").find("span.p_1").show().siblings("span.p_2").hide();
}
*/

//价格带
function pushPriceZone(min_price,max_price){
    $(".price_info").find("span.p_2").children().eq(0).html(toThousands(min_price,2));
    $(".price_info").find("span.p_2").children().eq(1).html(toThousands(max_price,2));
    $(".price_info").find("span.p_2").show().siblings("span.p_1").hide();
}

//促销
function getSale(spec,color,main_goods_id){
    if(spec == undefined || spec == '' || spec == null){
        spec = 0;
    }
    if(color == undefined || color == '' || color == null){
        color = 0;
    }
    $.post("/goods/sale-list",{spec:spec,main_goods_id:main_goods_id,spec_color:color},success,'json');
    function success(data){
        if(data.code == 200){

            var result = data.val;
            if(result.length == 0){
                if($('.detail_price').hasClass('detail_price_2')){

                }else{
                    $('.detail_price').addClass('detail_price_2');
                }

                if($('.cx_info').hasClass('hide')){

                }else{
                    $('.cx_info').addClass('hide');
                }
            }else{
                if($('.detail_price').hasClass('detail_price_2')){
                    $('.detail_price').removeClass('detail_price_2');
                }
                if($('.cx_info').hasClass('hide')){
                    $('.cx_info').removeClass('hide');
                }
                $('#sale_info').empty();
                $('#sale_info').append('<option selected value="">请选择促销活动</option>');
                //mod by FDG367 2018-02-07 495 【商城需求】商品图片上增加促销打标 start
                if(0<result.length){
                    var cx_info_html = '<label class="fl">促销:</label>';
                    cx_info_html += '<span class="price pr pr price_t"><span class="item_tips_span activity_message_span_'+result[0].activity_identification_style+'">'+result[0].activity_identification+'</span><span class="item_info  item_info_span item_info_span_'+result[0].activity_identification_style+'">'+result[0].activity_identification+'</span><span class="icon mid icon-arrow-down"></span></span>';
                    cx_info_html += '<ul class="select_ul cx_info_select" style="display: none;">';
                    
                    var restart = 0;
                    for(var i=0;i<result.length;i++){
                        if(i==restart){
                            cx_info_html+='<li data-attr-info="item_info_span_'+result[i].activity_identification_style+'" data-attr="activity_message_span_'+result[i].activity_identification_style+'" class="item item_tips pr selected activity_message_li activity_message_'+result[i].activity_identification_style+'">';
                        }else{
                            cx_info_html+='<li data-attr-info="item_info_span_'+result[i].activity_identification_style+'"  data-attr="activity_message_span_'+result[i].activity_identification_style+'" class="item item_tips pr activity_message_li activity_message_'+result[i].activity_identification_style+'">';
                        }
                        cx_info_html+='<span class="item_tips_span ">'+result[i].activity_identification+'</span>';
                        cx_info_html+='<a href="'+result[i].activity_url+'" class="item_info  item_info_span item_info_span_'+result[0].activity_identification_style+'">'+result[i].title+'</a>';
                        cx_info_html+='</li>';
                    }
                    cx_info_html+='</ul>';
                    $('.cx_info').html(cx_info_html);
                    selectedinfo();
                }
                //mod by FDG367 2018-02-07 495 【商城需求】商品图片上增加促销打标 start
            }
            $('#sale_info').on('change',function(){
            	var url = $(this).val();
            	if(url != ''){
            		window.open(url);
            	}
            });

        }
    }
}

//优惠券
function getCoupon(spec,color,main_goods_id){
    if(spec == undefined || spec == '' || spec == null){
        spec = 0;
    }
    if(color == undefined || color == '' || color == null){
        color = 0;
    }
    $.post("/goods/get-coupon",{spec:spec,main_goods_id:main_goods_id,spec_color:color},success,'json');
    function success(data){
        if(data.code == 200){
            var result = data.val;
            var i = 0;
            if(result.length > 0){
            	var html = '';
            	result.forEach(function(e){
            		if(parseInt(e.release_number) > parseInt(e.draw_count)){
            			var discount_html = '';
                		discount_html = (e.type == '3' ? '' : '¥')+'<span class="h1">'+e.highlight+'</span>';
                		html += '<input name="coupon_id[]" value="'+e.id+'" type="hidden"/><li class="coupon_id_'+e.id+' item '+coupon_class[e.type]+'">'+
                        '<div class="item_info">'+
                        '<h1 class=" yhj_price">'+discount_html+'</h1>'+
                        '<span class="yhj_price_go">'+e.title+'</span>'+
                        '<time class="yhj_time">有效期至:'+e.coupon_end_time+'</time>'+
                        '<div class="item_layer ">'+
                        '<p class="yhj_info  yhj_info_hide">'+e.content+'</p>'+
                        '<span class="icon fa fa-info-circle "></span>'+
                        '</div>'+
                        '<div class="yhj_tips"></div>'+
                        '</div>'+
                        '</li>';
                		i++;
            		}
            	});
            }
            if(i == 0){
            	$('.get-coupon').addClass('hide');
            	$('.js_inset_hart').css('margin-left','45px');
            }else{
            	$('#yhj_layer').find('.user_yhj_item').html(html);
            	$('.get-coupon').removeClass('hide');
            	$('.js_inset_hart').css('margin-left','');
            }
            $(".user_yhj_item .item").each(function () {
                $(this).find('.item_layer').hover(function () {
                    var tipsInfo = $(this).parents('.item').find('.yhj_info').text();
                    var tipsBox = $(this).parents('.item').find('.yhj_tips');
                    layer.tips(tipsInfo,tipsBox, {
                        tips: [1, '#FEFBEA'],
                        time: 2000
                    });
                },function(){

                })
            });
        }
    }
}

//格式化金额
function toThousands(s,n){
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse(),
        r = s.split(".")[1];
    t = "";
    for(i = 0; i < l.length; i ++ )
    {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    //return t.split("").reverse().join("") + "." + r;
    return t.split("").reverse().join("");
}

//type=1 规格   type=2 颜色
/*function getPrice(spec_color,type){
    var spec_price = new Array();
    if(type == 1){
        $('.spec_detail_span').each(function(){
            var str = $(this).attr('data-value');
            console.log(str);
            var goods_data = splitString(str);
            if(eval(spec_color) == eval(goods_data[1])){
                spec_price.push(goods_data[3]);
            }
        })
    }else{
        $('.spec_detail_span').each(function(){
            var str = $(this).attr('data-value');
            console.log(str);
            var goods_data = splitString(str);
            if(eval(spec_color) == eval(goods_data[2])){
                spec_price.push(goods_data[3]);
            }
        })
    }
    return spec_price;
}*/
//0:goods_id  1:spec  2:spec_color  3:sale_price  4:market_price
/*function getSpec(spec,color){
    var price = new Array();
    $('.spec_detail_span').each(function(){
        var str = $(this).attr('data-value');
        console.log(str);
        var goods_data = splitString(str);
        if(eval(goods_data[2]) == 0){
            if(eval(spec) == eval(goods_data[1])){
                price = goods_data[3];
            }
        }else{
            if(eval(spec) == eval(goods_data[1]) && eval(goods_data[2]) == eval(color)){
                price = goods_data;
            }
        }
    });
    return price;
}*/
/*

function splitString(string){
    if (string!==null) {
        return string.split("|");
    }else{

    }
}
*/

function detailbtns(){
    //下载优惠劵
//    $('.js_down').click(function(){
//        $.layerShow("领取优惠劵成功","请会员中心查看已加入的优惠劵，跳转下载优惠劵页面");
//    });
	//下载优惠劵
    $('.js_down').on('click',function(){
    	var coupon_id = [];
    	$('#yhj_layer').find('input[name="coupon_id[]"]').each(function(){
    		if($(this).val() != ''){
    			coupon_id.push($(this).val());
    		}
    	});
    	if(coupon_id.length == 0){
    		$('#yhj_layer').fadeIn(300);
	        $('html').addClass('noscroll');
	        $(window).scrollTop(100);
    	}else{
    		$.post('/goods/receive-coupon',{coupon_id:coupon_id},success,'json');
        	function success(data){
        		if (data.code == 200 || data.code == 300) {
        			$('#yhj_layer').fadeIn(300);
        	        $('html').addClass('noscroll');
        	        $(window).scrollTop(100);
        	        var show_coupon = data.val;
    	        	if(show_coupon.length > 0){
    	        		if(show_coupon.length == $('.detail-yhj').find('li').length){
    	        			$('#yhj_layer').find('.title').children('h2').text('优惠劵领取失败');
            	        	$('.detail-yhj').addClass('hide');
            	        	$('.user_yhj_list').html('<p class="f-go link-text" style="text-align: center;">无可领取优惠券</p>');
    	        		}else{
    	        			show_coupon.forEach(function(e,index){
        	        			$('.detail-yhj').find('.coupon_id_'+e).addClass('hide');
        	        		});
    	        		}
    	        	}
        		}else if(data.code == 404){
        			window.location.href="/login/index";
        		}else{
        			alert(data.val);
        		}
        	}
    	}
        $('#yhj_close').on('click',function(){
            $('#yhj_layer').fadeOut(300);
            $('html').removeClass('noscroll');
            var spec = $('#spece_max').val();
            var color = $('#spec_color').find('.color_active').attr('g_color');
            getCoupon(spec,color,main_goods_id);
        })
    });
    //加入心愿单
    $('.js_inset_hart').click(function(){
        // $('.js_inset_hart').children('.icon').animate({
        //     'transform':'scale(1.5)'
        // })
        var goods_id  =$('.main_goods_span').attr('data-shop');
        $.post('/goods/goods-collection',{goods_id:goods_id},function success(data) {
            if (data.code == 200) {
                // mod FDG416 2018/2/1 ID485 【商城需求】商品详情页按钮调整 start
                // mod FDG416 2018/2/7 ID496 【商城需求】修改商品详情页加入心愿单的按钮与动效 start
                $('.js_inset_hart').children('.icon').css({
                    'background':'url('+image_bath_url+'/static/pc/image/list/xin-over.png) no-repeat center',
                    '-webkit-transform': 'scale(1.3)',
                    '-moz-transform': 'scale(1.3)',
                    '-ms-transform': 'scale(1.3)',
                    'transform': 'scale(1.3)'
                });
                setTimeout(function(){
                    $('.js_inset_hart').children('.icon').css({
                        '-webkit-transform': 'scale(1.2)',
                        '-moz-transform': 'scale(1.2)',
                        '-ms-transform': 'scale(1.2)',
                        'transform': 'scale(1.2)'
                    });	
                },400);
                setTimeout(function(){
                    $('.js_inset_hart').children('.icon').css({
                        '-webkit-transform': 'scale(1.1)',
                        '-moz-transform': 'scale(1.1)',
                        '-ms-transform': 'scale(1.1)',
                        'transform': 'scale(1.1)'
                    });	
                },450);
                setTimeout(function(){
                    $('.js_inset_hart').children('.icon').css({
                        '-webkit-transform': 'scale(1)',
                        '-moz-transform': 'scale(1)',
                        '-ms-transform': 'scale(1)',
                        'transform': 'scale(1)'
                    });	
                },500);
                var countSum = parseInt($("#hart-count").text());
                countSum++;
                $("#hart-count").text(countSum);
                //回传后端处理
                $.layerShow("添加心愿单成功", "您已成功添加此商品到心愿单，请到会员中心查看");
            }
            if(data.code == 202){
                $('.js_inset_hart').children('.icon').css({
                    'background':'url('+image_bath_url+'/static/pc/image/list/xin.png) no-repeat center',
                    '-webkit-transform': 'scale(1.3)',
                    '-moz-transform': 'scale(1.3)',
                    '-ms-transform': 'scale(1.3)',
                    'transform': 'scale(1.3)'
                });
                setTimeout(function(){
                    $('.js_inset_hart').children('.icon').css({
                        '-webkit-transform': 'scale(1.2)',
                        '-moz-transform': 'scale(1.2)',
                        '-ms-transform': 'scale(1.2)',
                        'transform': 'scale(1.2)'
                    });	
                },400);
                setTimeout(function(){
                    $('.js_inset_hart').children('.icon').css({
                        '-webkit-transform': 'scale(1.1)',
                        '-moz-transform': 'scale(1.1)',
                        '-ms-transform': 'scale(1.1)',
                        'transform': 'scale(1.1)'
                    });	
                },450);
                setTimeout(function(){
                    $('.js_inset_hart').children('.icon').css({
                        '-webkit-transform': 'scale(1)',
                        '-moz-transform': 'scale(1)',
                        '-ms-transform': 'scale(1)',
                        'transform': 'scale(1)'
                    });	
                },500);
                // mod FDG416 2018/2/7 ID496 【商城需求】修改商品详情页加入心愿单的按钮与动效 end
                
                // mod FDG416 2018/2/1 ID485 【商城需求】商品详情页按钮调整 end
                var countSum = parseInt($("#hart-count").text());
                countSum--;
                $("#hart-count").text(countSum);
                //回传后端处理
                $.layerShow("删除心愿单成功","您已成功在心愿单中删除此商品，请到会员中心查看");
            }
            if(data.code == 500){
                window.location.href = data.url;
            }
            if(data.code == 502 || data.code == 503){
                $.layerShow("操作失败", data.val);
            }
        },'json');
    });
    //相关搭配 加入心愿单
    //mod by FDG367 461 点击商品图片、国家图标、品牌、商品名称、系列名、价格，都能链接至商品详情页 2018-01-24 start
    $('#group_goods_list').find('span.icon_animation').on('click',function(){
        var goods_id = $(this).attr('data-goods-id');
        $.post('/goods/goods-collection',{goods_id:goods_id},function success(data){
            if(data.code == 500){
                window.location.href = data.url;
            }
            if(data.code == 200 || data.code == 202){
                var count = parseInt($("#hart-count").text());
                $('#group_goods_list').find('span.icon_animation').each(function(){
                    if($(this).attr('data-goods-id') == goods_id){
                        if($(this).is(".icon-hart-active")){
                            count--;
                            $(this).removeClass('icon-hart-active').addClass('icon-hart');
                            $("#hart-count").text(count);
                        }else {
                            $(this).removeClass('icon-hart').addClass('icon-hart-active');
                            gohartAnimate(this);
                        }
                    }
                });
            }
        },'json');
    });
    //mod by FDG367 461 点击商品图片、国家图标、品牌、商品名称、系列名、价格，都能链接至商品详情页 2018-01-24 end
    //分享
    $('.js_fx').click(function(){
        if($(this).parents().find(".wechat_fx_layer").is('.hide')){
            $(".btn-group .wechat_fx_layer").removeClass("hide").addClass('show');
            //$.layerShow("分享成功","亲爱的上帝，感谢您的分享！");
        }else {
            $(".btn-group .wechat_fx_layer").addClass('hide').removeClass("show")
        }
    })

}

//add by FDG367 461 点击商品图片、国家图标、品牌、商品名称、系列名、价格，都能链接至商品详情页 2018-01-24 end
function gohartAnimate(dom){
    var countSum = parseInt($("#hart-count").text());
    var goodsIcon = $(dom);
    var goodsOffset = goodsIcon.offset();
    var flyIcon = goodsIcon.clone();
    $('body').append(flyIcon);
    countSum++;
    $("#hart-count").text(countSum);
    var count = $('#hart-count').offset();
    flyIcon.css({ 'z-index': 9000,
        'display': 'block',
        'position': 'absolute',
        'top': goodsOffset.top +'px',
        'left': goodsOffset.left +'px',
        'width': goodsIcon.width() +'px',
        'height': goodsIcon.height() +'px'
    });
    flyIcon.animate({
        top: count.top,
        left: count.left,
        width: 10,
        height: 10
    }, 'slow',function(){
        flyIcon.remove()
    });
}
//add by FDG367 461 点击商品图片、国家图标、品牌、商品名称、系列名、价格，都能链接至商品详情页 2018-01-24 end
function selected(infoBox,itemUl){
    var itemT =  itemUl.children('.selected').find('.item_tips_span');
    //console.log(itemUl.children('.selected').attr('data-attr'));
    var itemI =  itemUl.children('.selected').find('.item_info');
    var itemTips = itemT.text();
    var itemInfo = itemI.text();

    infoBox.find('.item_info').text(itemInfo);
    infoBox.on('click',function(){
        itemUl.fadeIn(300);
        $(this).find('.icon').removeClass('icon-arrow-down').addClass('icon-arrow-up')
    });
    itemUl.children('.item').each(function(){
        $(this).on('click',function(event){
            event.preventDefault();
            $(this).addClass('selected').siblings('.item').removeClass('selected');

            //console.log( );
            var itemTips = itemUl.children('.selected').find('.item_tips_span').text();
            var itemInfo = itemUl.children('.selected').find('.item_info').text();
            infoBox.find('.item_tips_span').removeAttr('class').addClass('item_tips_span').addClass($(this).attr('data-attr')).text(itemTips);
            infoBox.find('.item_info').removeAttr('class').addClass('item_info item_info_span').addClass($(this).attr('data-attr-info')).text(itemInfo);
            infoBox.find('.item_info').text(itemInfo);
            itemUl.fadeOut(300);
            infoBox.find('.icon').removeClass('icon-arrow-up').addClass('icon-arrow-down');
            if(infoBox.find('.item_tips_span').text()==''){
                infoBox.removeClass('price_t')
            }else {
                infoBox.addClass('price_t')
            }
            if(itemUl.children('.selected').find('.item_info').attr('href')){
                window.open(itemUl.children('.selected').find('.item_info').attr('href'));
            }


        })
    })
};
//促销信息选择
function selectedinfo(){
    selected($('.price'),$('.cx_info_select'));
    //selected($('#style_list_box'),$('.style_list_select'));
}
//mod by FDG367 2018-02-07 495 【商城需求】商品图片上增加促销打标 start
//$(function(){selectedinfo()})
//mod by FDG367 2018-02-07 495 【商城需求】商品图片上增加促销打标 end
